package com.example.rocketmqspringbootdemo.service.consumer;

import com.example.rocketmqspringbootdemo.CommonConstrant;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

@Service
@RocketMQMessageListener(topic = CommonConstrant.RocketMQ.COLLECTION_TOPIC,
        consumerGroup = CommonConstrant.RocketMQ.POOL_STRING_BATCH_CONSUMER_GROUP)
public class CollectionTopicConsumer implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
    @Override
    public void onMessage(String msg) {
        System.out.println("[COLLECTION_TOPIC]received: " + msg + "-----at " + LocalTime.now());
    }

    //用于更细致的参数设置
    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        //每次消费的最大消息数，默认为1
        consumer.setConsumeMessageBatchMaxSize(5);
        //每次拉取时间间隔
        consumer.setPullInterval(5000);
        //每次从一个队列中拉取的消息数，
        consumer.setPullBatchSize(2);
    }
}
